<template>
  <el-dialog
    close="el-dia"
    title="新增大标题"
    :visible="dialogVisible"
    width="30%"
    :modal=false
    :show-close=false
  >
    <!-- 主题内容 -->
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          type="text"
          v-model="ruleForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>

    <!-- 确认取消按钮 -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel1">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { mapState } from 'vuex'

// 引入增加大标题接口
import getclassifyr from '@/api/getclassifyr'

export default {
  name: 'Pop',
  data() {
    // 表单验证规则
    var validateName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('输入内容不能为空'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        name: ''
      },
      rules: {
        name: [{ validator: validateName, trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapState(['dialogVisible'])
  },
  methods: {
    // 点击确定
    submitForm() {
      if (this.ruleForm.name != '') {
        // 调用新增大标题接口
        getclassifyr
          .addclassify(this.$store.state.token, this.ruleForm.name)
          .then((res) => {
            if (res.data.status == 'success') {
              this.$message({
                message: res.data.msg,
                type: 'success'
              })
              this.$store.commit('cancelButton1')
              location.reload()
            }
          })
      } else {
        this.$message.error('添加失败')
      }
    },
    // 点击取消
    cancel1() {
      this.$store.commit('cancelButton')
    }
  }
}
</script>

<style scoped>
.el-dia {
  border-radius: 10px;
}
</style>